﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace teach
{
    public partial class ChoicesPanel : PanelBase
    {
        private float FontSize = 27.5F;

        public ChoicesPanel()
        {
            InitializeComponent();
        }

        private float GetFont(string data)
        {
            float size = 36;
            if (data.Length > 85)
            {
                size = 12;
            }
            else if (data.Length > 80)
            {
                size = 14;
            }
            else if (data.Length > 70)
            {
                size = 16;
            }
            else if (data.Length > 55)
            {
                size = 20;
            }
            else if (data.Length > 40)
            {
                size = 22;
            }
            else if (data.Length > 25)
            {
                size = 24;
            }
            else if (data.Length > 20)
            {
                size = 26;
            }
            else if (data.Length > 14)
            {
                size = 28;
            }
            return size;
        }

        private float GetButtonFont(string data)
        {
            float size = 21;
            if (data.Length > 20)
            {
                size = 20;
            }
            else if (data.Length > 12)
            {
                size = 20;
            }
            return size;
        }

        /// <summary>
        /// Set the operation question running
        /// </summary>
        /// <param name="question"></param>
        public override void SetQuestion(Question question)
        {
            Reset();
            m_answered = false;
            m_currentQuestion = question;
            UpdateFont(question.Properties["question"]);
            txtQuestion.Text = question.Properties["question"];
            //txtQuestion.Font = new System.Drawing.Font("Microsoft Sans Serif", GetFont(txtQuestion.Text), System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            
            int choicesCount = 0;
            if (question.Properties.ContainsKey("answerA"))
            {
                choicesCount++;
            }
            if (question.Properties.ContainsKey("answerB"))
            {
                choicesCount++;
            }
            if (question.Properties.ContainsKey("answerC"))
            {
                choicesCount++;
            }
            if (question.Properties.ContainsKey("answerD"))
            {
                choicesCount++;
            }
            int yy = 144;
            switch (choicesCount)
            {
                case 2:
                    btnAnswer1.Location = new System.Drawing.Point(264, yy);
                    btnAnswer2.Location = new System.Drawing.Point(499, yy);
                    btnAnswer3.Visible = false;
                    btnAnswer4.Visible = false;
                    SetButtonModelForTwo(question.Properties["answerA"], question.Properties["answerB"]);
                    break;
                case 3:
                    btnAnswer1.Location = new System.Drawing.Point(365, yy);
                    btnAnswer2.Location = new System.Drawing.Point(647, yy);
                    btnAnswer3.Location = new System.Drawing.Point(77, yy);
                    btnAnswer3.Visible = true;
                    btnAnswer4.Visible = false;
                    SetButtonModelForThree(question.Properties["answerA"],question.Properties["answerB"],question.Properties["answerC"]);
                    break;
                case 4:
                    btnAnswer1.Location = new System.Drawing.Point(264, yy);
                    btnAnswer2.Location = new System.Drawing.Point(499, yy);
                    btnAnswer3.Location = new System.Drawing.Point(29, yy);
                    btnAnswer4.Location = new System.Drawing.Point(734, yy);
                    btnAnswer3.Visible = true;
                    btnAnswer4.Visible = true;
                    SetButtonModelForFour(question.Properties["answerA"],question.Properties["answerB"],question.Properties["answerC"],question.Properties["answerD"]);
                    break;
            }
            btnAnswer1.Font = new System.Drawing.Font("Microsoft Sans Serif", GetButtonFont(btnAnswer1.Text), System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            btnAnswer2.Font = new System.Drawing.Font("Microsoft Sans Serif", GetButtonFont(btnAnswer2.Text), System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            btnAnswer3.Font = new System.Drawing.Font("Microsoft Sans Serif", GetButtonFont(btnAnswer3.Text), System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            btnAnswer4.Font = new System.Drawing.Font("Microsoft Sans Serif", GetButtonFont(btnAnswer4.Text), System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));

            this.timeoutMeter.Interval = question.Timeout;
            this.timeoutMeter.Enabled = true;
        }

        private void SetButtonModelForTwo(string text1, string text2)
        {
            int rndNum = TeachController.RND.Next(2);
            switch (rndNum)
            {
                case 0:
                    btnAnswer1.Text = text1;
                    btnAnswer2.Text = text2;
                    break;
                case 1:
                    btnAnswer1.Text = text2;
                    btnAnswer2.Text = text1;
                    break;
            }
        }

        private void SetButtonModelForThree(string text1, string text2, string text3)
        {
            int rndNum = TeachController.RND.Next(6);
            switch (rndNum)
            {
                case 0:
                    btnAnswer1.Text = text1;
                    btnAnswer2.Text = text2;
                    btnAnswer3.Text = text3;
                    break;
                case 1:
                    btnAnswer1.Text = text1;
                    btnAnswer2.Text = text3;
                    btnAnswer3.Text = text2;
                    break;
                case 2:
                    btnAnswer1.Text = text2;
                    btnAnswer2.Text = text1;
                    btnAnswer3.Text = text3;
                    break;
                case 3:
                    btnAnswer1.Text = text2;
                    btnAnswer2.Text = text3;
                    btnAnswer3.Text = text1;
                    break;
                case 4:
                    btnAnswer1.Text = text3;
                    btnAnswer2.Text = text1;
                    btnAnswer3.Text = text2;
                    break;
                case 5:
                    btnAnswer1.Text = text3;
                    btnAnswer2.Text = text2;
                    btnAnswer3.Text = text1;
                    break;
            }
        }

        private void UpdateFont(string text)
        {
            Size size = TextRenderer.MeasureText(text, txtQuestion.Font, new Size(txtQuestion.Width, txtQuestion.Height), TextFormatFlags.WordBreak);
            if (size.Height > txtQuestion.Height)
            {
                FontSize = FontSize - 2;
                this.txtQuestion.Font = new System.Drawing.Font("Segoe UI", FontSize, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                UpdateFont(text);
            }
        }

        private void SetButtonModelForFour(string text1, string text2, string text3, string text4)
        {
            int rndNum = TeachController.RND.Next(6);
            switch (rndNum)
            {
                case 0:
                    btnAnswer1.Text = text1;
                    btnAnswer2.Text = text2;
                    btnAnswer3.Text = text3;
                    btnAnswer4.Text = text4;
                    break;
                case 1:
                    btnAnswer1.Text = text4;
                    btnAnswer2.Text = text2;
                    btnAnswer3.Text = text3;
                    btnAnswer4.Text = text1;
                    break;
                case 2:
                    btnAnswer1.Text = text2;
                    btnAnswer2.Text = text1;
                    btnAnswer3.Text = text4;
                    btnAnswer4.Text = text3;
                    break;
                case 3:
                    btnAnswer1.Text = text3;
                    btnAnswer2.Text = text4;
                    btnAnswer3.Text = text2;
                    btnAnswer4.Text = text1;
                    break;
                case 4:
                    btnAnswer1.Text = text1;
                    btnAnswer2.Text = text4;
                    btnAnswer3.Text = text2;
                    btnAnswer4.Text = text3;
                    break;
                case 5:
                    btnAnswer1.Text = text2;
                    btnAnswer2.Text = text3;
                    btnAnswer3.Text = text1;
                    btnAnswer4.Text = text4;
                    break;
            }
        }

        public override Control GetAnswerControl()
        {
            return btnAnswer1;
        }

        public override void SetStatusImage(bool success, Image image)
        {
            pctStatus.Image = image;
            pctStatus.Refresh();
            Color col = Color.Blue;
            if (!success)
            {
                col = Color.Red;
            }

            if (btnAnswer1.Text == m_currentQuestion.Answer.ToString())
            {
                btnAnswer1.ForeColor = col;
            }
            else if (btnAnswer2.Text == m_currentQuestion.Answer.ToString())
            {
                btnAnswer2.ForeColor = col;
            }
            else if (btnAnswer3.Text == m_currentQuestion.Answer.ToString())
            {
                btnAnswer3.ForeColor = col;
            }
            else if (btnAnswer4.Text == m_currentQuestion.Answer.ToString())
            {
                btnAnswer4.ForeColor = col;
            }
        }

        private void Reset()
        {
            btnNext.Visible = false;
            pctStatus.Image = null;
            pctStatus.Refresh();
            btnAnswer1.ForeColor = Color.Black;
            btnAnswer2.ForeColor = Color.Black;
            btnAnswer3.ForeColor = Color.Black;
            btnAnswer4.ForeColor = Color.Black;
        }

        private void Boutton_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            this.timeoutMeter.Enabled = false;
            if (!m_answered)
            {
                m_answered = true;
                base.RaiseAnswer(btn.Text);
                btnNext.Visible = true;
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            m_answered = false;
            Reset();
            base.RaiseNextClicked();
            btnNext.Visible = false;
        }
    }
}
